from rest_framework import serializers

from .models import MovieDetail, MovieCountry, SubTitle, MovieStaff, MovieStaffCategory, MovieKindCategory, \
    MovieTypeCategory, FilmRowPiece, MoviePieceSeat

from user_movie_operations.models import UserFavMovie, UserStarMovie
from django.db.models import Avg


class MovieListSerializer(serializers.ModelSerializer):
    fav_num = serializers.SerializerMethodField(read_only=True)
    star_num = serializers.SerializerMethodField(read_only=True)

    def get_star_num(self, obj):
        score = UserStarMovie.objects.filter(movie=obj).aggregate(Avg('star'))
        return round(score['star__avg'], 1)

    def get_fav_num(self, obj):
        return UserFavMovie.objects.filter(movie=obj).count()

    class Meta:
        model = MovieDetail
        fields = ('name', 'status', 'user_want_num', 'fav_num', 'star_num')


class MovieCountrySerializer(serializers.ModelSerializer):
    class Meta:
        model = MovieCountry
        fields = "__all__"


class SubTitleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubTitle
        fields = "__all__"


class MovieStaffSerializer(serializers.ModelSerializer):
    class Meta:
        model = MovieStaff
        fields = "__all__"


class MovieStaffCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = MovieStaffCategory
        fields = "__all__"


class MovieKindCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = MovieKindCategory
        fields = "__all__"


class MovieTypeCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = MovieTypeCategory
        fields = "__all__"


class MovieDetailSerializer(serializers.ModelSerializer):
    movie_state = MovieCountrySerializer(many=True, read_only=True)
    sub_title = SubTitleSerializer(many=True, read_only=True)
    staffs = MovieStaffSerializer(many=True, read_only=True)
    kind_category = MovieKindCategorySerializer(many=True, read_only=True)
    type_category = MovieTypeCategorySerializer(many=True, read_only=True)

    fav_num = serializers.SerializerMethodField(read_only=True)
    star_num = serializers.SerializerMethodField(read_only=True)

    def get_star_num(self, obj):
        score = UserStarMovie.objects.filter(movie=obj).aggregate(Avg('star'))
        return round(score['star__avg'], 1)

    def get_fav_num(self, obj):
        return UserFavMovie.objects.filter(movie=obj).count()

    class Meta:
        model = MovieDetail
        fields = "__all__"


class MovieDetailUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = MovieDetail
        fields = "__all__"


class FilmRowPieceSerializer(serializers.ModelSerializer):
    class Meta:
        model = FilmRowPiece
        fields = "__all__"


class MoviePieceSeatSerializer(serializers.ModelSerializer):
    class Meta:
        model = MoviePieceSeat
        fields = "__all__"


class FilmRowPieceRetrieveSerializer(serializers.ModelSerializer):
    """
    电影排片详细信息,把片场对应的座位信息取出来
    """

    #需要使用到related_name
    movie_piece_seat_piece = MoviePieceSeatSerializer(read_only=True, many=True)

    class Meta:
        model = FilmRowPiece
        fields = ("movie_piece_seat_piece", 'id')
